home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / libtest / callbacks-example-baselinks.c next >
C/C++ Source or Header  |  2006-06-04  |  4KB  |  116 lines

  1. /*
  2.     HTTrack external callbacks example : enforce a constant base href
  3.     Can be useful to make copies of site's archives using site's URL base href as root reference
  4.     .c file
  5.  
  6.     How to build: (callback.so or callback.dll)
  7.       With GNU-GCC:
  8.         gcc -O -g3 -Wall -D_REENTRANT -shared -o mycallback.so callbacks-example.c -lhttrack1
  9.       With MS-Visual C++:
  10.         cl -LD -nologo -W3 -Zi -Zp4 -DWIN32 -Fe"mycallback.dll" callbacks-example.c libhttrack1.lib
  11.  
  12.       Note: the httrack library linker option is only necessary when using libhttrack's functions inside the callback
  13.  
  14.     How to use:
  15.       httrack --wrapper mycallback ..
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. /* Standard httrack module includes */
  23. #include "httrack-library.h"
  24. #include "htsopt.h"
  25. #include "htsdefines.h"
  26.  
  27. /* Local function definitions */
  28. static int process_file(t_hts_callbackarg *carg, httrackp* opt, char* html, int len, const char* url_address, const char* url_file);
  29. static int check_detectedlink(t_hts_callbackarg *carg, httrackp* opt, char* link);
  30. static int check_detectedlink_end(t_hts_callbackarg *carg, httrackp *opt);
  31.  
  32. /* 
  33. module entry point 
  34. */
  35. EXTERNAL_FUNCTION int hts_plug(httrackp *opt, const char* argv) {
  36.   const char *arg = strchr(argv, ',');
  37.   if (arg != NULL)
  38.     arg++;
  39.  
  40.   /* Check args */
  41.   fprintf(stderr, "Plugged..\n");
  42.   if (arg == NULL || *arg == '\0' || strlen(arg) >= HTS_URLMAXSIZE / 2) {
  43.     fprintf(stderr, "** callback error: arguments expected or bad arguments\n");
  44.     fprintf(stderr, "usage: httrack --wrapper modulename,base\n");
  45.     fprintf(stderr, "example: httrack --wrapper callback,http://www.example.com/\n");
  46.     return 0;  /* failed */
  47.   } else {
  48.     char *callbacks_userdef = strdup(arg);      /* userdef */
  49.  
  50.     /* Plug callback functions */
  51.     CHAIN_FUNCTION(opt, check_html, process_file, callbacks_userdef);
  52.     CHAIN_FUNCTION(opt, linkdetected, check_detectedlink, callbacks_userdef);
  53.     CHAIN_FUNCTION(opt, end, check_detectedlink_end, callbacks_userdef);
  54.  
  55.     fprintf(stderr, "Using root '%s'\n", callbacks_userdef);
  56.   }
  57.  
  58.   return 1;  /* success */
  59. }
  60.  
  61. static int process_file(t_hts_callbackarg *carg, httrackp* opt, char* html, int len, const char* url_address, const char* url_file) {
  62.   char* prevBase;
  63.  
  64.   /* Call parent functions if multiple callbacks are chained. */
  65.   if (CALLBACKARG_PREV_FUN(carg, check_html) != NULL) {
  66.     if (!CALLBACKARG_PREV_FUN(carg, check_html)(CALLBACKARG_PREV_CARG(carg), opt, html, len, url_address, url_file)) {
  67.       return 0;  /* Abort */
  68.     }
  69.   }
  70.  
  71.   /* Disable base href, if any */
  72.   if ( ( prevBase = strstr(html, "<BASE HREF=\"") ) != NULL) {
  73.     prevBase[1] = 'X';
  74.   }
  75.  
  76.   return 1;  /* success */
  77. }
  78.  
  79. static int check_detectedlink(t_hts_callbackarg *carg, httrackp* opt, char* link) {
  80.   const char *base = (char*) CALLBACKARG_USERDEF(carg);
  81.  
  82.   /* Call parent functions if multiple callbacks are chained. */
  83.   if (CALLBACKARG_PREV_FUN(carg, linkdetected) != NULL) {
  84.     if (!CALLBACKARG_PREV_FUN(carg, linkdetected)(CALLBACKARG_PREV_CARG(carg), opt, link)) {
  85.       return 0;  /* Abort */
  86.     }
  87.   }
  88.  
  89.   /* The incoming (read/write) buffer is at least HTS_URLMAXSIZE bytes long */
  90.   if (strncmp(link, "http://", 7) == 0 || strncmp(link, "https://", 8) == 0) {
  91.     char temp[HTS_URLMAXSIZE * 2];
  92.     strcpy(temp, base);
  93.     strcat(temp, link);
  94.     strcpy(link, temp);
  95.   }
  96.  
  97.   return 1;  /* success */
  98. }
  99.  
  100. static int check_detectedlink_end(t_hts_callbackarg *carg, httrackp *opt) {
  101.   char *base = (char*) CALLBACKARG_USERDEF(carg);
  102.  
  103.   fprintf(stderr, "Unplugged ..\n");
  104.   if (base != NULL) {
  105.     free(base);
  106.     base = NULL;
  107.   }
  108.  
  109.   /* Call parent functions if multiple callbacks are chained. */
  110.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  111.     return CALLBACKARG_PREV_FUN(carg, end)(CALLBACKARG_PREV_CARG(carg), opt);
  112.   }
  113.  
  114.   return 1;  /* success */
  115. }
  116.